﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class LayEquipmentCommands : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new System.ArgumentNullException();
				}
				UIApplication application = commandData.Application;
				UIDocument activeUIDocument = application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				List<ViewType> list = new List<ViewType>();
                list.Add((ViewType)1);
                list.Add((ViewType)2);
                list.Add((ViewType)4);
				if (!YJKRevitTools.ViewCheck(application.ActiveUIDocument, list))
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					for (;;)
					{
						HYEquipmentCommon hyequipmentCommon = new HYEquipmentCommon();
						ICollection<ElementId> allFamilyInstance = hyequipmentCommon.GetAllFamilyInstance(document, BuiltInCategory.OST_MechanicalEquipment);
						SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
						FormLayWaterTrank formLayWaterTrank = new FormLayWaterTrank(document);
						DialogResult dialogResult = formLayWaterTrank.ShowDialog();
						if (dialogResult == DialogResult.OK)
						{
							WatertankParam watertankParam = formLayWaterTrank.watertankParam;
							FamilySymbol familySymbol = formLayWaterTrank.fs;
							if (familySymbol == null)
							{
								familySymbol = spaceFamilyDataReader.GetFamilySymbolById(formLayWaterTrank.id, "EquipmentWaterTank", document, BuiltInCategory.OST_MechanicalEquipment);
							}
							if (familySymbol != null)
							{
								try
								{
									activeUIDocument.PromptForFamilyInstancePlacement(familySymbol);
								}
								catch (Exception)
								{
								}
							}
							ICollection<ElementId> allFamilyInstance2 = hyequipmentCommon.GetAllFamilyInstance(document, BuiltInCategory.OST_MechanicalEquipment);
							new List<ElementId>();
							foreach (ElementId item in allFamilyInstance.Intersect(allFamilyInstance2))
							{
								allFamilyInstance2.Remove(item);
							}
							transaction = new Transaction(document, "布置水箱");
							transaction.Start();
							foreach (ElementId elementId in allFamilyInstance2)
							{
								Element elementById = document.GetElementById(elementId);
								if (watertankParam.Width > 0.0)
								{
									elementById.GetParameter("宽度").Set(watertankParam.Width / 304.8);
								}
								if (watertankParam.Heigth > 0.0)
								{
									elementById.GetParameter("高度").Set(watertankParam.Heigth / 304.8);
								}
								if (watertankParam.Length > 0.0)
								{
									elementById.GetParameter("长度").Set(watertankParam.Length / 304.8);
								}
								if (watertankParam.Inpipe > 0.0)
								{
									elementById.GetParameter("进水口直径").Set(watertankParam.Inpipe / 304.8);
								}
								if (watertankParam.Outpipe > 0.0)
								{
									elementById.GetParameter("出水口直径").Set(watertankParam.Outpipe / 304.8);
								}
								if (watertankParam.Overpipe > 0.0)
								{
									elementById.GetParameter("溢流管直径").Set(watertankParam.Overpipe / 304.8);
								}
								if (watertankParam.Drainpipe > 0.0)
								{
									elementById.GetParameter("泄水管直径").Set(watertankParam.Drainpipe / 304.8);
								}
								Parameter parameter = null;
								Parameter parameter2 = null;
								double num = 0.0;
								if (elementById.GetParameter(BuiltInParameter.INSTANCE_ELEVATION_PARAM) != null)
								{
									parameter2 = elementById.GetParameter(BuiltInParameter.INSTANCE_ELEVATION_PARAM);
								}
								else
								{
									Parameter parameter3 = elementById.GetParameter(BuiltInParameter.FAMILY_LEVEL_PARAM);
									if (parameter3 != null)
									{
										parameter3.Set(document.ActiveView.GenLevel.Id);
									}
								}
								if (parameter2 != null)
								{
									parameter2.Set(UnitConvert.CovertToAPI(watertankParam.Offset * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2));
									num = parameter2.AsDouble();
								}
								if (num != UnitConvert.CovertToAPI(watertankParam.Offset * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2))
								{
									if (elementById.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM) != null)
									{
										parameter = elementById.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM);
									}
									if (parameter != null)
									{
										parameter.Set(UnitConvert.CovertToAPI(watertankParam.Offset * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2));
									}
								}
							}
							document.Regenerate();
							transaction.Commit();
						}
						else if (dialogResult == DialogResult.Cancel)
						{
							break;
						}
					}
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
